package com.yx.pc.api.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.TbAuthorApply;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.service.PcTbAuthorApplyService;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 普通作者申请
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-02 10:26:26
 */
@RestController
@RequestMapping("pc/api/authorapply")
public class PcTbAuthorApplyController {
	@Autowired
	private PcTbAuthorApplyService authorApplyService;
	
	/**
	 * 根据用户显示申请列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params, @LoginUser TbUser tbUser){
		//查询列表数据
        Query query = new Query(params);
        query.put("userid",tbUser.getUserId());

		List<TbAuthorApply> authorApplyList = authorApplyService.queryList(query);
		int total = authorApplyService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(authorApplyList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{ id}")
	public R info(@PathVariable("id") Integer  id){
		TbAuthorApply authorApply = authorApplyService.queryObject( id);
		
		return R.ok().put("authorApply", authorApply);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TbAuthorApply authorApply){
		authorApplyService.save(authorApply);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TbAuthorApply authorApply){
		authorApplyService.update(authorApply);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[]  ids){
		authorApplyService.deleteBatch( ids);
		
		return R.ok();
	}

	/*
	* 申请成为普通作者
	*/
	@RequestMapping("/apply")
	public R apply(@LoginUser TbUser tbUser,String text) throws UnsupportedEncodingException {
		Assert.isBlank(text,"内容不能为空");
		if (tbUser.getIdentity()==1||tbUser.getIdentity()==2){
			return R.error("你已经是作者了");
		}
		//首先查询作者之前的申请
		List<TbAuthorApply> tbAuthorApplies = authorApplyService.findAllByUser(tbUser.getUserId());
		//循环判断之前的申请
		for (TbAuthorApply tbAuthorApply:tbAuthorApplies){
			if (tbAuthorApply.getStatus()==0){
				return R.error("之前的申请还在审核中，请勿重复申请");
			}
		}
		//初始化作者申请对象
		TbAuthorApply tbAuthorApply = new TbAuthorApply();
		//赋值
		tbAuthorApply.setUserid(tbUser.getUserId());
		if (tbUser.getEmail()!=null){
			tbAuthorApply.setEmail(tbUser.getEmail());
		}
		String arr = new String(Base64.decodeBase64(text),"utf-8");
		tbAuthorApply.setText(arr);
		tbAuthorApply.setCreatedate(new Date());
		tbAuthorApply.setNickname(tbUser.getNickname());
		tbAuthorApply.setUsername(tbUser.getMobile());
		tbAuthorApply.setPassword(tbUser.getPassword());
		tbAuthorApply.setStatus(0);
		tbAuthorApply.setType(0);
		authorApplyService.save(tbAuthorApply);
		return R.ok();
	}

	/*
	* 查看申请状态
	*/
	@RequestMapping("/applyInfo")
	public R applyInfo(@LoginUser TbUser tbUser){
		/*if (tbUser.getIdentity()==1 || tbUser.getIdentity()==2){
			return R.error("你已经是作者了");
		}*/
		//创建一个申请记录
		TbAuthorApply tbAuthorApply = null;
		//查询申请记录
		List<TbAuthorApply> authorApplies = authorApplyService.findAllByUser(tbUser.getUserId());
		//判断
		if (authorApplies.size()!=0){
			tbAuthorApply = authorApplies.get(0);
		}
		return R.ok().put("date",tbAuthorApply);
	}
}
